package cn.xuyang520.tyim.sys.controller;

import cn.xuyang520.tyim.sys.config.TySysShiroRealm;
import cn.xuyang520.tyim.sys.domain.TySysUser;
import cn.xuyang520.tyim.sys.domain.TySysUserRepository;
import cn.xuyang520.tyim.sys.core.TyCache;
import cn.xuyang520.tyim.sys.service.TySysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;


/**
 * sys模块公用controller
 */
@SuppressWarnings("all")
@Controller
@RequestMapping
public class TySysController extends TySysBaseController implements ErrorController {
    @Autowired
    private TySysUserRepository tySysUserRepository;
    @Autowired
    private TyCache tyCache;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private TySysUserService tySysUserService;

    /**
     * 首页
     */
    @RequestMapping
    public String index(ModelMap map, HttpSession session) {
        TySysUser user = (TySysUser) session.getAttribute("user");
        map.put("username", user.getTbName());
        map.put("groupname", "未定义");
        return "index";
    }


    @GetMapping("/login")
    public String login() {
        System.out.println("get登陆");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            return "redirect:/";
        }
        return "login";
    }

    @PostMapping("/login")
    //@ResponseBody
    public String login(String username, String password, ModelMap map) {
        // 改为全部抛出异常，避免ajax csrf token被刷新
        System.out.println("post登陆");
        if (StringUtils.isEmpty(username)) {
            map.put("err", "用户名不能为空");
        }
        if (StringUtils.isEmpty(password)) {
            map.put("err", "密码不能为空");
        }

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 设置记住密码
        //token.setRememberMe(1 == rememberMe);
        try {
            subject.login(token);
            TySysUser user = (TySysUser) subject.getPrincipals().fromRealm(TySysShiroRealm.class.getName()).iterator().next();//获取session中的用户
            subject.getSession().setAttribute("user", user);

        } catch (UnknownAccountException e) {
            map.put("err", "账号不存在！");
        } catch (DisabledAccountException e) {
            map.put("err", "账号未启用！");
        } catch (IncorrectCredentialsException e) {
            map.put("err", "密码错误！");
        } catch (Throwable e) {
            e.printStackTrace();
            map.put("err", "无法登陆！");
        }
        if (map.size() == 0)
            return "redirect:/login";
        return "login";
    }

    @RequestMapping("/error")
    public String error() {
        return "404";
    }

    @RequestMapping(ERROR_PATH)
    public String handleError(){
        return "404";
    }
    private static final String ERROR_PATH = "/error2";
    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

}
